#include "MazeWars.h"
#include "SpellManager.h"

//
#include "State_Menu.h"
#include "State_Initialize.h"
#include "TwoPlayer.h"
//#include "ThreePlayer.h"
//#include "FourPlayer.h"
//


//JSL 1/16/08------------------------------------------------------------
extern IDirectInputDevice8		*mJoystick[2];
extern DIJOYSTATE2				mJoystickState;
//extern IDirectInputDevice8 *mJoystick2;
extern 	ID3DXSprite* mSprite;
//-----------------------------------------------------------------------

MazeWars::MazeWars(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
{
	//make sure the computer has the proper hardware for our settings
	if(!checkDeviceCaps())
	{
		MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
	}

	//this creates a new class for the font class (well what will become our font class)
	mGfxStats = new GfxStats();

	//obtain a pointer to a ID3DXSprite class, mSprite being the pointer
	HR(D3DXCreateSprite(gd3dDevice, &mSprite));


		// 	//added 11/26/07
	halfTileWidth = 32.0f;
	tileWidth = 64.0f;
	// Adds in Direct Sound
	sound.SetWindowHandle(mhMainWnd);
	sound.InitDirectSound();

	//load the textures for each sprite
	//this is where you would load any sprites you are using at the beginning of the game or perhaps for the whole game
	//we may want to modify this, but we will see how it goes, the 3 lines below can be deleted but i left them
	//for reference
	HR(D3DXCreateTextureFromFile(gd3dDevice, "MazeMenuTest01.png", &mBkgdTex));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Cursor.png", &mMenuCursor));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "TilePallet.png", &mTile));
	
	//added 10/21/07
	HR(D3DXCreateTextureFromFile(gd3dDevice, "CooldTilePallet.png", &mCoolDownPallet));

	//HR(D3DXCreateTextureFromFile(gd3dDevice, "LifeBar.png", &mLifeBar));
	//HR(D3DXCreateTextureFromFile(gd3dDevice, "GSP2.png", &mAvaGuy));
	//HR(D3DXCreateTextureFromFile(gd3dDevice, "GSP1.png", &mAvaGirl));

	// added 11/01/07 ZH
	//HR(D3DXCreateTextureFromFile(gd3dDevice, "FireBall_01.png", &mFireballR1Anime));

	
	//set the centers of each object, centers are in image coordinates, so if image is 512x512, center is 256x256
	//this is setting the centers for each image, if we make a tile class, we may modify this part so it is more 
	//automated, but for now simply use the center of each sprite, again, left the lines below for reference
	mBkgdCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	mMnuCurCenter = D3DXVECTOR3(halfTileWidth, halfTileWidth, 0.0f);

	//mLifeBarCenter = D3DXVECTOR3(256.0f, halfTileWidth, 0.0f);
	mLifeBarPos = D3DXVECTOR3(0.0f, -300.0f, 0.0f);

	/*mAvaGuyCenter = D3DXVECTOR3(halfTileWidth, tileWidth, 0.0f);
	mAvaGuyPos = D3DXVECTOR3(-620.0f, 0.0f, 0.0f);

	mAvaGirlCenter = D3DXVECTOR3(halfTileWidth, tileWidth, 0.0f);
	mAvaGirlPos = D3DXVECTOR3(620.0f, 0.0f, 0.0f);*/

	//added 11/01/07
//	mFireballR1AnimeCenter = D3DXVECTOR3(tileWidth, tileWidth, 0.0f);
//	mFireballR1AnimePos = D3DXVECTOR3(-540.0f,0.0f,0.0f);
//	spellCurrFrame = 0;
//	spellFinalFrames = false;
	//game cursor info

	HR(D3DXCreateTextureFromFile(gd3dDevice, "cursoranimation3.png", &gameCursor));
	gameCursorCenter = D3DXVECTOR3(halfTileWidth, halfTileWidth, 0.0f);
	gameCursorPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	gameCursorPosition2 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	currFrame = 0;
	
	//set the position of the objects to 0
	//self explanatory
	mMnuCurPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	CURSOR_POSITION = 0;
	
	anime = false;
	DXA = 0;
	DYA = 0;



	createTrunk();


//	ai.getNewTarget();


	currState = State_Init::Instance();
	TwoPlayer::Instance();
	State_Single::Instance();
	GameOverState::Instance();

//	FourPlayer::Instance();

	//we call this because we want the device states to be set during construction as well as when a device is reset
	onResetDevice();
}

MazeWars::~MazeWars()
{
	delete mGfxStats;

	//be sure to release all objects, since i commented out most of the above, i need to make changes here
	ReleaseCOM(mSprite);
	ReleaseCOM(mBkgdTex);
	ReleaseCOM(mMenuCursor);
	ReleaseCOM(mTile);
	ReleaseCOM(gameCursor);


	//added 10/21/07
	ReleaseCOM(mCoolDownPallet);

//	ReleaseCOM(mFireballR1Anime);




}

bool MazeWars::checkDeviceCaps()
{
	// Nothing to check.
	return true;
}

void MazeWars::onLostDevice()
{
	//this is where we put any code that needs to be executed before the rendering device is reset
	mGfxStats->onLostDevice();
	HR(mSprite->OnLostDevice());
}

void MazeWars::onResetDevice()
{
	//call the onResetDevice of other objects
	mGfxStats->onResetDevice();
	HR(mSprite->OnResetDevice());

	//sets up the camera 1000 units back looking at the origin
	D3DXMATRIX V;
	D3DXVECTOR3 pos(0, 0, -1000.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXMatrixLookAtLH(&V, &pos, &target, &up);
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &V));

	//The following code defines the volume of space the camera sees
	D3DXMATRIX P;
	RECT R;
	GetClientRect(mhMainWnd, &R);
	float width = (float)R.right;
	float height = (float)R.bottom;
	D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 1.0f, 5000.0f);
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &P));

	//this code sets texture filters, which helps to smooth out distortions when you scale a texture
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));

	//this line of code disables Direct3D Lighting
	HR(gd3dDevice->SetRenderState(D3DRS_LIGHTING, false));

	//WE DO ALPHA TESTING AND BLENDING SO THAT THE BLACK BORDER AROUND THE SHIP IN THIS CASE DOES NOT SHOW
	//the following code specifies an alpha test and reference value
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAREF, 10));
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER));

	//the following code is used to set up alpha blending 
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE));
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1));
	HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
	HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

	//indicates that we are using 2D texture coordinates
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2));
}

void MazeWars::updateScene(float dt)
{
	//this is somewhat how font is used, i will add a different function tho that is separate, but this
	//will give you a good idea if you look at the update function or display function in GfxStats
	//I will add another function to actually draw, but I wanted to try and get you a basic working bare bone
	//model so you could start looking around and see what is needed to get the window up adn then see what
	//is needed to actually add things such as sprites and what not

	//this function basically updates everything. It polls the keyboard, updates statistics and graphics, etc
	//two triangles for each sprite--two for background, two for ship, and two for each bullet.  Similarly, 4 vertices 
	//for each sprite.

	//notice how u update, then draw, order is important
	//mGfxStats->setTriCount(4 + mBulletList.size()*2);
	//mGfxStats->setVertexCount(8 + mBulletList.size()*4);
	mGfxStats->update(dt);


	//not needed for basic game
	//Update Game Objects.
	currState->Execute(gd3dApp);
	
	if (currState == TwoPlayer::Instance())
	{
		
		//human = true;
		updateMenu(dt);
		updateCursor(dt, 1);
		updateCursor(dt, 2);
		//updateAnime();
		//if(player.getSpellAlive())
		//	updateAnime(dt);
		for(int i = 0; i < NUMPLAYERS; i++)
			player[i]->updateCoolDowns(dt);

		SpellManager::instance()->updateSpell(dt, player);
	}

	if (currState == State_Single::Instance())
	{
		player[0]->setSingleHuman();
		//player[1]->setSingleHuman();
		//player[2]->setSingleHuman();

		updateMenu(dt);
		updateCursor(dt, 1);
		updateCursor(dt, 2);
		player[0]->updateCoolDowns(dt);
		//updateAnime();
		//if(player.getSpellAlive())
		//	updateAnime(dt);
		SpellManager::instance()->updateSpell(dt, player);
	}

}

//this function is very important, although it will change greatly for our game
void MazeWars::drawScene()
{
	D3DVIEWPORT9 player2ViewPort;
	D3DVIEWPORT9 player1ViewPort;
	D3DVIEWPORT9 combatViewPort;

	if (currState == State_Menu::Instance())
	{
		//gd3dDevice->SetViewport( &MenuViewPort );
		//D3DXMATRIX texScaling;
		//D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		//HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		//D3DXMATRIX T, S;
		//D3DXMatrixTranslation(&T, -260.0f, -140.0f, 0.0f);
		//D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
		//HR(mSprite->SetTransform(&(S*T)));

		////draw the menu
		//HR(mSprite->Draw(mBkgdTex, 0, &mBkgdCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
		//HR(mSprite->Flush());

		////restore the texture coordinate scaling transform. mirror texture 
		////coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
		//D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	}

	if (currState == TwoPlayer::Instance())
	{

		player1ViewPort.X      = 0;
		player1ViewPort.Y      = 0;
		player1ViewPort.Width  = d3dPP.BackBufferWidth / 2;
		player1ViewPort.Height = d3dPP.BackBufferHeight * .75;
		player1ViewPort.MinZ   = 0.0f;
		player1ViewPort.MaxZ   = 1.0f;

		player2ViewPort.X      = d3dPP.BackBufferWidth / 2;
		player2ViewPort.Y      = 0;
		player2ViewPort.Width  = d3dPP.BackBufferWidth / 2;
		player2ViewPort.Height = d3dPP.BackBufferHeight * .75;
		player2ViewPort.MinZ   = 0.0f;
		player2ViewPort.MaxZ   = 1.0f;

		combatViewPort.X	   = 0;
		combatViewPort.Y	   = d3dPP.BackBufferHeight * .75;
		combatViewPort.Width   = d3dPP.BackBufferWidth;
		combatViewPort.Height  = d3dPP.BackBufferHeight * .25;
		combatViewPort.MinZ	   = 0.0f;
		combatViewPort.MaxZ	   = 0.0f;


		
	gd3dDevice->SetViewport( &player1ViewPort );
		// Now we can clear just view-port's portion of the buffer to red...
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));


		HR(gd3dDevice->BeginScene());
	    
			//draw stuff here
			HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));


		



			mGfxStats->display();
			
			drawTile(1);
			drawBranches(1);
			drawGameCursor(1);
			//drawMenu();
			//drawCursor();

			//anything that needs to be drawn should be above this line and below the mSprite->Begin line
			HR(mSprite->End());
	    
		gd3dDevice->EndScene();

		//clear the backbuffer and depth buffer
		//HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		//Now draw right side

		gd3dDevice->SetViewport( &player2ViewPort );
		

		// Now we can clear just view-port's portion of the buffer to green...
		//gd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		//                     D3DCOLOR_COLORVALUE( 0.0f, 1.0f, 0.0f, 1.0f ), 1.0f, 0 );
		HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		gd3dDevice->BeginScene();
		HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
			

			//mGfxStats->display();
			
			drawTile(2);
			drawBranches(2);
			drawGameCursor(2);

			//drawLifeBar(ai);
			//ai.addHealth(-1);
			//drawMenu();
			//drawCursor();

		//anything that needs to be drawn should be above this line and below the mSprite->Begin line
		HR(mSprite->End());

		gd3dDevice->EndScene();
		
 		gd3dDevice->SetViewport( &combatViewPort );
	/*
		gd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
							D3DCOLOR_COLORVALUE( 0.0f, 1.0f, 0.0f, 1.0f ), 1.0f, 0 );
	*/
		HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		gd3dDevice->BeginScene();
		HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
			
			drawLifeBar(player[0], -310, 700);
			drawPlayer(player[0]);
			drawLifeBar(player[1], 310, 700);
			drawPlayer(player[1]);

			SpellManager::instance()->drawSpell(mSprite);
			//drawLifeBar(player2, 310, 700);
			//drawAva(mAvaGirl, mAvaGirlPos);

		//	if(player.getSpellAlive())
		//	{
		//		drawAnime();
				//player.setCastSpell(false);
		//	}

	

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	gd3dDevice->EndScene();
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
	}


if (currState == State_Single::Instance())
	{

		player1ViewPort.X      = 0;
		player1ViewPort.Y      = 0;
		player1ViewPort.Width  = d3dPP.BackBufferWidth / 2;
		player1ViewPort.Height = d3dPP.BackBufferHeight * .75;
		player1ViewPort.MinZ   = 0.0f;
		player1ViewPort.MaxZ   = 1.0f;

		player2ViewPort.X      = d3dPP.BackBufferWidth / 2;
		player2ViewPort.Y      = 0;
		player2ViewPort.Width  = d3dPP.BackBufferWidth / 2;
		player2ViewPort.Height = d3dPP.BackBufferHeight * .75;
		player2ViewPort.MinZ   = 0.0f;
		player2ViewPort.MaxZ   = 1.0f;

		combatViewPort.X	   = 0;
		combatViewPort.Y	   = d3dPP.BackBufferHeight * .75;
		combatViewPort.Width   = d3dPP.BackBufferWidth;
		combatViewPort.Height  = d3dPP.BackBufferHeight * .25;
		combatViewPort.MinZ	   = 0.0f;
		combatViewPort.MaxZ	   = 0.0f;


		
			gd3dDevice->SetViewport( &player1ViewPort );
		// Now we can clear just view-port's portion of the buffer to red...
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));


		HR(gd3dDevice->BeginScene());
	    
			//draw stuff here
			HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));


		



			mGfxStats->display();
			
			drawTile(1);
			drawBranches(1);
			drawGameCursor(1);
			//drawMenu();
			//drawCursor();

			//anything that needs to be drawn should be above this line and below the mSprite->Begin line
			HR(mSprite->End());
	    
		gd3dDevice->EndScene();

		//clear the backbuffer and depth buffer
		//HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		//Now draw right side

		gd3dDevice->SetViewport( &player2ViewPort );
		

		// Now we can clear just view-port's portion of the buffer to green...
		//gd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		//                     D3DCOLOR_COLORVALUE( 0.0f, 1.0f, 0.0f, 1.0f ), 1.0f, 0 );
		HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		gd3dDevice->BeginScene();
		HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
			

			//mGfxStats->display();
			
			drawTile(2);
			drawBranches(2);
			drawGameCursor(2);

			//drawLifeBar(ai);
			//ai.addHealth(-1);
			//drawMenu();
			//drawCursor();

		//anything that needs to be drawn should be above this line and below the mSprite->Begin line
		HR(mSprite->End());

		gd3dDevice->EndScene();
		
 		gd3dDevice->SetViewport( &combatViewPort );
	/*
		gd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
							D3DCOLOR_COLORVALUE( 0.0f, 1.0f, 0.0f, 1.0f ), 1.0f, 0 );
	*/
		HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

		gd3dDevice->BeginScene();
		HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
			
			drawLifeBar(player[0], -310, 700);
			drawPlayer(player[0]);
			drawLifeBar(player[2], 310, 700);
			drawPlayer(player[2]);

			SpellManager::instance()->drawSpell(mSprite);
			//drawLifeBar(player2, 310, 700);
			//drawAva(mAvaGirl, mAvaGirlPos);

		//	if(player.getSpellAlive())
		//	{
		//		drawAnime();
				//player.setCastSpell(false);
		//	}

	

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	gd3dDevice->EndScene();
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
	}

}

void MazeWars::drawCursor()
{
	//without transformation, image will draw at center of screen
	D3DXMATRIX T, S;
	//new game is -250, 105
	//continue is -250, 45
	//end is -250, -10
	D3DXMatrixTranslation(&T, mMnuCurPos.x, mMnuCurPos.y, 0.0f);
	D3DXMatrixScaling(&S, 0.7f, 0.7f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(mMenuCursor, 0, &mMnuCurCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
}


void MazeWars::drawTile(int play)
{

	if (currState == TwoPlayer::Instance())
	{
		if(play == 1)
		{
			//do we need to draw this every loop? or only if something causes the tile to change?
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
			
			//for each tile. . .

			player[0]->setTrunkIteratorBegin();
			
			while(player[0]->getTrunkIteratorEnd())
			{
				//for each rank, we need to set tile position, then flush
				D3DXMATRIX T, M;

				D3DXMatrixTranslation(&T, ((player[0]->getTrunkIteratorX()) + player[0]->getdx()), (player[0]->getTrunkIteratorY() + player[0]->getdy()), 0.0f) ;
				HR(mSprite->SetTransform(&(T)));
				
				HR(mSprite->Draw(mTile, &player[0]->getTrunkIteratorRECT(), &player[0]->getTrunkIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));

				player[0]->increaseTrunkIterator();
			}

			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		}
		else
		{
			//do we need to draw this every loop? or only if something causes the tile to change?
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
			
			//for each tile. . .

			player[1]->setTrunkIteratorBegin();
			
			while(player[1]->getTrunkIteratorEnd())
			{
				//for each rank, we need to set tile position, then flush
				D3DXMATRIX T, M;

				D3DXMatrixTranslation(&T, ((player[1]->getTrunkIteratorX()) + player[1]->getdx()), (player[1]->getTrunkIteratorY() + player[1]->getdy()), 0.0f) ;
				HR(mSprite->SetTransform(&(T)));

				HR(mSprite->Draw(mTile, &player[1]->getTrunkIteratorRECT(), &player[1]->getTrunkIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));

				player[1]->increaseTrunkIterator();
			}

			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}
	}
	if (currState == State_Single::Instance())
	{
		if(play == 1)
		{
			//do we need to draw this every loop? or only if something causes the tile to change?
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
			
			//for each tile. . .

			player[0]->setTrunkIteratorBegin();
			
			while(player[0]->getTrunkIteratorEnd())
			{
				//for each rank, we need to set tile position, then flush
				D3DXMATRIX T, M;

				D3DXMatrixTranslation(&T, ((player[0]->getTrunkIteratorX()) + player[0]->getdx()), (player[0]->getTrunkIteratorY() + player[0]->getdy()), 0.0f) ;
				HR(mSprite->SetTransform(&(T)));
				
				HR(mSprite->Draw(mTile, &player[0]->getTrunkIteratorRECT(), &player[0]->getTrunkIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));

				player[0]->increaseTrunkIterator();
			}

			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		}
		else
		{
			//do we need to draw this every loop? or only if something causes the tile to change?
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
			
			//for each tile. . .

			player[2]->setTrunkIteratorBegin();
			
			while(player[2]->getTrunkIteratorEnd())
			{
				//for each rank, we need to set tile position, then flush
				D3DXMATRIX T, M;

				D3DXMatrixTranslation(&T, ((player[2]->getTrunkIteratorX()) + player[2]->getdx()), (player[2]->getTrunkIteratorY() + player[2]->getdy()), 0.0f) ;
				HR(mSprite->SetTransform(&(T)));

				HR(mSprite->Draw(mTile, &player[2]->getTrunkIteratorRECT(), &player[2]->getTrunkIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));

				player[2]->increaseTrunkIterator();
			}

			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}
	}

}

void MazeWars::drawGameCursor(int player)
{

	if(player == 1)
	{
		//enable transparency
		HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));

		//draw cursor
		D3DXMATRIX Trans;
		D3DXMatrixTranslation(&Trans, gameCursorPosition.x, gameCursorPosition.y, 0.0f);
		HR(mSprite->SetTransform(&Trans));
		//int row = currFrame/8; //row
		int col = currFrame%59; 

		RECT R = {col * tileWidth, 0, (col + 1) * tileWidth, tileWidth};
		HR(mSprite->Draw(gameCursor, &R, &gameCursorCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));

		HR(mSprite->Flush());
		
		HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));

	}
	else
	{

		//enable transparency
		HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));

		//draw cursor
		D3DXMATRIX Trans;
		D3DXMatrixTranslation(&Trans, gameCursorPosition2.x, gameCursorPosition2.y, 0.0f);
		HR(mSprite->SetTransform(&Trans));
		//int row = currFrame/8; //row
		int col = currFrame%59; 

		RECT R = {col * tileWidth, 0, (col + 1) * tileWidth, tileWidth};
		HR(mSprite->Draw(gameCursor, &R, &gameCursorCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));

		HR(mSprite->Flush());
		
		HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
	}
}
void MazeWars::drawBranches(int playerNum)
{
	int nbrtest = 0;
	int nbrtest2 = 0;

	if (currState == TwoPlayer::Instance())
	{
		if(playerNum == 1)
		{


			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

			player[0]->setBranchIteratorBegin();

			while(player[0]->getBranchIteratorEndIf())
					{
						//for each rank, we need to set tile position, then flush
						D3DXMATRIX T, M;

						D3DXMatrixTranslation(&T, (player[0]->getBranchIteratorX() + player[0]->getdx()), (player[0]->getBranchIteratorY() + player[0]->getdy()), 0.0f);

						HR(mSprite->SetTransform(&(T)));
			
						//added 10/21/07
						if(player[0]->getBranchIteratorIsCoolDown())
						{
							//HR(mSprite->Draw(mCoolDownPallet, &branchIterator->getRect(), &branchIterator->getCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							HR(mSprite->Draw(mCoolDownPallet, &player[0]->getBranchIteratorRECT(), &player[0]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}
						else
						{
							HR(mSprite->Draw(mTile, &player[0]->getBranchIteratorRECT(), &player[0]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							//HR(mSprite->Draw(mTile, &player.getBranchIteratorRect(), &player.getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}

						player[0]->incrementBranchIterator();

						nbrtest++;
					
					}

			//draw spell map
			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}
		else
		{
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

			player[1]->setBranchIteratorBegin();

			while(player[1]->getBranchIteratorEndIf())
					{
						//for each rank, we need to set tile position, then flush
						D3DXMATRIX T, M;

						D3DXMatrixTranslation(&T, (player[1]->getBranchIteratorX() + player[1]->getdx()), (player[1]->getBranchIteratorY() + player[1]->getdy()), 0.0f);

						HR(mSprite->SetTransform(&(T)));
			
						//added 10/21/07
						if(player[1]->getBranchIteratorIsCoolDown())
						{
							//HR(mSprite->Draw(mCoolDownPallet, &branchIterator->getRect(), &branchIterator->getCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							HR(mSprite->Draw(mCoolDownPallet, &player[1]->getBranchIteratorRECT(), &player[1]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}
						else
						{
							HR(mSprite->Draw(mTile, &player[1]->getBranchIteratorRECT(), &player[1]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							//HR(mSprite->Draw(mTile, &player.getBranchIteratorRect(), &player.getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}

						player[1]->incrementBranchIterator();

						nbrtest2++;
					
					}

			//draw spell map
			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}
	}
	if (currState == State_Single::Instance())
	{
		if(playerNum == 1)
		{


			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

			player[0]->setBranchIteratorBegin();

			while(player[0]->getBranchIteratorEndIf())
					{
						//for each rank, we need to set tile position, then flush
						D3DXMATRIX T, M;

						D3DXMatrixTranslation(&T, (player[0]->getBranchIteratorX() + player[0]->getdx()), (player[0]->getBranchIteratorY() + player[0]->getdy()), 0.0f);

						HR(mSprite->SetTransform(&(T)));
			
						//added 10/21/07
						if(player[0]->getBranchIteratorIsCoolDown())
						{
							//HR(mSprite->Draw(mCoolDownPallet, &branchIterator->getRect(), &branchIterator->getCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							HR(mSprite->Draw(mCoolDownPallet, &player[0]->getBranchIteratorRECT(), &player[0]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}
						else
						{
							HR(mSprite->Draw(mTile, &player[0]->getBranchIteratorRECT(), &player[0]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							//HR(mSprite->Draw(mTile, &player.getBranchIteratorRect(), &player.getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						}

						player[0]->incrementBranchIterator();

						nbrtest++;
					
					}

			//draw spell map
			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}
		else
		{
			D3DXMATRIX texScaling;
			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

			player[2]->setBranchIteratorBegin();

			while(player[2]->getBranchIteratorEndIf())
					{
						//for each rank, we need to set tile position, then flush
						D3DXMATRIX T, M;

						D3DXMatrixTranslation(&T, (player[2]->getBranchIteratorX() + player[2]->getdx()), (player[2]->getBranchIteratorY() + player[2]->getdy()), 0.0f);

						HR(mSprite->SetTransform(&(T)));
			
						//added 10/21/07
						//if(player[1]->getBranchIteratorIsCoolDown())
						//{
							//HR(mSprite->Draw(mCoolDownPallet, &branchIterator->getRect(), &branchIterator->getCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						//	HR(mSprite->Draw(mCoolDownPallet, &ai->getBranchIteratorRECT(), &ai->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						//}
						//else
						//{
							HR(mSprite->Draw(mTile, &player[2]->getBranchIteratorRECT(), &player[2]->getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
							//HR(mSprite->Draw(mTile, &player.getBranchIteratorRect(), &player.getBranchIteratorCenter(), 0, D3DCOLOR_XRGB(255, 255, 255)));
						//}
						player[2]->incrementBranchIterator();
						nbrtest2++;
					}

			//draw spell map
			HR(mSprite->Flush());

			D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
			HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
		}

	}

}
void MazeWars::drawMenu()
{
	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -225.0f, 0.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.5f, 0.5f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(mBkgdTex, 0, &mBkgdCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());

	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));
}

void MazeWars::updateMenu(float dt)
{
	static float cursorDelay = 0.0f;

	cursorDelay += dt;
	//Need to test cursor keys on a different laptop
/*	if((gDInput->keyDown(DIK_W)) && cursorDelay > 0.2f)
	{
		//decrement cursor position
		CURSOR_POSITION-= 1;
		
		if(CURSOR_POSITION == -1) CURSOR_POSITION = 2;

	//	//reset delay
	//	cursorDelay = 0.0f;
	//}

	//if(gDInput->keyDown(DIK_S)&& cursorDelay > 0.2f)
	//{
	//	//increment cursor position
	//	CURSOR_POSITION += 1;

	//	if(CURSOR_POSITION == 3) CURSOR_POSITION = 0;

		//reset delay
		cursorDelay = 0.0f;
	}*/

	//switch(CURSOR_POSITION)
	//{
	//	//new game is -250, 105
	//	//continue is -250, 45
	//	//end is -250, -10
	//	
	//	case 0:
	//		mMnuCurPos = D3DXVECTOR3(-250.0f, 105.0f, 0.0f);
	//		break;

	//	case 1:
	//		mMnuCurPos = D3DXVECTOR3(-250.0f, 45.0f, 0.0f);
	//		break;

	//	case 2:
	//		mMnuCurPos = D3DXVECTOR3(-250.0f, -10.0f, 0.0f);
	//		break;

	//	default:
	//		mMnuCurPos = D3DXVECTOR3(-250.0f, 105.0f, 0.0f);
	//		break;
	//}
}

RECT MazeWars::updateTile()
{
	
	RECT R = {0, 192, 64, 256};

	int x = rand()%6;

	switch(x)
	{
	case 0:
		R.bottom = 0;
		R.left = 192;
		R.right = 64;
		R.top = 256;
		break;
	case 1:
		R.bottom = 64;
		R.left = 192;
		R.right = 128;
		R.top = 256;
		break;
	case 2:
		R.bottom = 128;
		R.left = 192;
		R.right = 192;
		R.top = 256;
		break;
	case 3:
		R.bottom = 192;
		R.left = 192;
		R.right = 256;
		R.top = 256;
		break;
	case 4:
		R.bottom = 64;
		R.left = 128;
		R.right = 128;
		R.top = 192;
		break;
	case 5:
		R.bottom = 128;
		R.left = 128;
		R.right = 192;
		R.top = 192;
		break;
	case 6:
		R.bottom = 192;
		R.left = 128;
		R.right = 256;
		R.top = 192;
		break;
	}
	return R;
}

//void MazeWars::initIt()
//{
//	player.increaseTrunkIterator();
//	player.increaseTrunkIterator();
//}


//added 10/21/07
void MazeWars::setCoolDown(int nodeID, int rank, int play)
{

//if(play == 1)
//{
//	std::list<Branch>::iterator i = player.getBranchIteratorBegin();
//	while(i != player.getBranchIteratorEnd())
//	{
//		if(i->getNodeID() == nodeID && i->getRank() <= rank)
//		{
//			i->setCoolDown(true);
//		}
//
//		i++;
//	}
//}
//else
//{
	// Something... cool will happen.. <3
//}

}


void MazeWars::updateCursor(float dt, int playerNum)
{
	static int prevJSButtons[12] = {0};
	static int prevJSButtons2[12] = {0};
		
	if (playerNum == 1)
	{
		static float cursorDelay = 0.0f;
		static float timeAccum = 0.0f;
		static int prevJS = 0;
		DIJOYSTATE2 js;
		//mGfxStats->setNext(dx);
		
		timeAccum += dt;
		//animate cursor
		if(timeAccum >= 1.0f/30.0f )
		{
			++currFrame;
			timeAccum = 0.0f;

			if(currFrame > 58)
				currFrame = 0;
		}
		
		//1/11/07 JSL----------------------------------------------------------------------------------------
		mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
		//if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] <= 0) && cursorDelay > 0.2f)
		if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
		{
			if(player[0]->moveCursor(Up, player[0]))
			{
				//cursorDelay = 0.0f;
				sound.PlaySound(CursorSound);
			}
		}
		
		//else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] <= 9000) && cursorDelay > 0.2f)
		else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
		{
			if(player[0]->moveCursor(Right, player[0]))
			{
				cursorDelay = 0.0f;
				sound.PlaySound(CursorSound);
			}
		}

		//else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] <= 18000) && cursorDelay > 0.2f)
		else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
		{
			if(player[0]->moveCursor(Down, player[0]))
			{
				cursorDelay = 0.0f;
				sound.PlaySound(CursorSound);
			}
		}

		//else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] <= 27000) && cursorDelay > 0.2f)
		else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
		{
			if(player[0]->moveCursor(Left, player[0]))
			{
				cursorDelay = 0.0f;
				sound.PlaySound(CursorSound);
			}
		}
		if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
		{
			if(player[0]->moveCursor(A, player[0]))
			{
				sound.PlaySound(CursorSound);
			}
		}

		if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
		{
			if(player[0]->moveCursor(S, player[0]))
			{
				sound.PlaySound(CursorSound);
			}
		}

		if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
		{
			if(player[0]->moveCursor(D, player[0]))
			{
				sound.PlaySound(CursorSound);
			}
		}

		if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
		{
			if(player[0]->moveCursor(W, player[0]))
			{
				sound.PlaySound(CursorSound);
			}
		}

	//------------------------------------------------------------------------------------------------------------
		//Quit Button. Take out of final version.
		if(js.rgbButtons[4] & 0x80)
			PostQuitMessage(0);
	//------------------------------------------------------------------------------------------------------------

		if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			prevJS = js.rgdwPOV[0];

		for(int i = 0; i < 12; i++)
		{
			prevJSButtons[i] = js.rgbButtons[i];
		}

	}
	else
	{
	
		if (currState == TwoPlayer::Instance())
		{

			static float cursorDelay2 = 0.0f;
			static float timeAccum2 = 0.0f;
			static int prevJS2 = 0;
			//mGfxStats->setNext(dx);
			DIJOYSTATE2 js2;

		//	timeAccum2 += dt;
		//	//animate cursor
		//	if(timeAccum2 >= 1.0f/30.0f )
		//	{
		////		++currFrame;
		//		timeAccum2 = 0.0f;

		////		if(currFrame > 58)
		////			currFrame = 0;
		//	}
			

			mJoystick[1]->GetDeviceState(sizeof(DIJOYSTATE2), &js2);
		
			if(((js2.rgdwPOV[0] != -1) && js2.rgdwPOV[0] == 0) && (js2.rgdwPOV[0] ^ prevJS2))
			{
				if(player[1]->moveCursor(Up,player[1]))
				{
					//MessageBox(0, "test fucker", "tst", 0);
					//cursorDelay = 0.0f;
					sound.PlaySound(CursorSound);
				}
			}
			
			else if(((js2.rgdwPOV[0] != -1) && js2.rgdwPOV[0] == 9000) && (js2.rgdwPOV[0] ^ prevJS2))
			{
				if(player[1]->moveCursor(Right,player[1]))
				{
					//cursorDelay2 = 0.0f;
					sound.PlaySound(CursorSound);
				}
			}

			else if(((js2.rgdwPOV[0] != -1) && js2.rgdwPOV[0] == 18000) && (js2.rgdwPOV[0] ^ prevJS2))
			{
				if(player[1]->moveCursor(Down,player[1]))
				{
					//cursorDelay2 = 0.0f;
					sound.PlaySound(CursorSound);
				}
			}

			else if(((js2.rgdwPOV[0] != -1) && js2.rgdwPOV[0] == 27000) && (js2.rgdwPOV[0] ^ prevJS2))
			{
				if(player[1]->moveCursor(Left,player[1]))
				{
					cursorDelay2 = 0.0f;
					sound.PlaySound(CursorSound);
				}
			}
			if((js2.rgbButtons[0] & 0x80) && (prevJSButtons2[0] ^ js2.rgbButtons[0]))
			{
				if(player[1]->moveCursor(A,player[1]))
				{
					sound.PlaySound(CursorSound);
				}
			}

			if(js2.rgbButtons[1] & 0x80 && (prevJSButtons2[1] ^ js2.rgbButtons[1]))
			{
				if(player[1]->moveCursor(S,player[1]))
				{
					sound.PlaySound(CursorSound);
				}
			}

			if(js2.rgbButtons[2] & 0x80 && (prevJSButtons2[2] ^ js2.rgbButtons[2]))
			{
				if(player[1]->moveCursor(D,player[1]))
				{
					sound.PlaySound(CursorSound);
				}
			}

			if(js2.rgbButtons[3] & 0x80 && (prevJSButtons2[3] ^ js2.rgbButtons[3]))
			{
				if(player[1]->moveCursor(W,player[1]))
				{
					sound.PlaySound(CursorSound);
				}
			}

		//------------------------------------------------------------------------------------------------------------
			//Quit Button. Take out of final version.
			if(js2.rgbButtons[4] & 0x80)
				PostQuitMessage(0);
		//------------------------------------------------------------------------------------------------------------

			if(!(js2.rgdwPOV[0] == 4500 || js2.rgdwPOV[0] == 13500 || js2.rgdwPOV[0] == 22500 || js2.rgdwPOV[0] == 31500))
				prevJS2 = js2.rgdwPOV[0];

			for(int i = 0; i < 12; i++)
			{
				prevJSButtons2[i] = js2.rgbButtons[i];
			}

		}

		if (currState == State_Single::Instance())
		{
			if(player[2]->updateMovement(player[2]))
			{
				//MessageBox(gd3dApp->getMainWindowHandle(), "Awesome the Ai is working....", "Error", 0);
			}
		}

	}
}

void MazeWars::createTrunk()
{
	//passing the id like this is horrible, will change if i have time
	player[0] = new Player("GSP2.png", -620.0f, 0.0f, PLAYER1, true);
	player[1] = new Player("GSP1.png", 620.0f, 0.0f, PLAYER2, true);
	//ai = new AI("GSP1.png", 620.0f, 0.0f, PLAYER2); 
	player[2] = new AI("GSP1.png", 620.0f, 0.0f, AIKUN, true); 

	player[0]->readerSetUp();
	player[1]->readerSetUp();
	player[2]->readerSetUp();
}



void MazeWars::drawLifeBar(Player *p, float x, float y)
{
	int health = 0;
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, mLifeBarPos.x + x, mLifeBarPos.y + y, 0.0f);
	D3DXMatrixScaling(&S, 1.0f, 4.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	health = p->getHealth();
	if(health > 100)
	{
		health = 100;
	}
	else if(health < 0)
	{
		GameOverState::Instance()->Hate(p->getPlayerID());

		gd3dApp->setState(GameOverState::Instance());

		health = 1;

	}

	//Left, Top, Right, Bottom
	RECT R = {0, 8192-(100-health + 1) * 32, 512, 8192 - (100 - health) * 32};
	
	HR(mSprite->Draw(p->getLifeBarTex(), &R, &(p->getLifeBarCenter()), 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
}

void MazeWars::drawPlayer(Player *p)
{
	//without transformation, image will draw at center of screen
	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, p->getPlayerPortraitPosX(), p->getPlayerPortraitPosY(), 0.0f);
	D3DXMatrixScaling(&S, 1.56f, 6.5f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	HR(mSprite->Draw(p->getPortraitTex(), 0, &(p->getPlayerPortraitCenter()), 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
}

int MazeWars::getPlayer1Health()
{
	return player[0]->getHealth();
}


// added 11/01/07
/*void MazeWars::playanime(int player, int spellNum)
{
	anime = true;
	if ( player == 1)
	{

		DXA = (int) ((640 / mGfxStats->getFPS()) * 4);
		if ( spellNum == 1)
		{
			mFireballR1AnimePos = D3DXVECTOR3(-540.0f,0.0f, 0.0f);
		}
		else
		{
		}

	}
	else
	{
		DXA = (int) ( -1 *((640 / mGfxStats->getFPS()) * 4));
	}


}*/

/*void MazeWars::drawAnime()
{
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
	//without transformation, image will draw at center of screen
	D3DXMATRIX T, S;
	//new game is -250, 105
	//continue is -250, 45
	//end is -250, -10	
	//mFireballR1AnimePos.x += mFireballR1AnimePos.x + DXA;
	D3DXMatrixTranslation(&T, mFireballR1AnimePos.x, mFireballR1AnimePos.y, 0.0f);
	D3DXMatrixScaling(&S, 2.0f, 8.0f, 1.0f);
	HR(mSprite->SetTransform(&(S*T)));

	int col = spellCurrFrame%11;
	int row = spellCurrFrame/4;
//	int row = 0;
	// Left, Top, Right, Bottom
	//RECT R = {0,384,128,512};
	RECT R = {col * tileWidth * 2, (512-(tileWidth * 2 * (row + 1))),  ((col + 1) * tileWidth * 2), (512-(tileWidth * 2 * row))};
/*	RECT R;
	R.left = col * tileWidth;
	R.top = (512-(tileWidth * (row + 1)));
	R.right = R.left + tileWidth;
	R.bottom = R.top + tileWidth;
	
	//Left, Top, Right, Bottom
	//RECT R = {0, 128, , 0};
	//HR(mSprite->Draw(gameCursor, &R, &gameCursorCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	//draw the menu
	HR(mSprite->Draw(mFireballR1Anime, &R, &mFireballR1AnimeCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
}*/
